/***************************************************************************\
 * The Mattise Kernel: Userland Software									*
 * Copyright 2007 Matthew Iselin											*
 * Licensed under the GPL													*
 *																			*
 * main.c																	*
 *																			*
 * Entrypoint for the Mattise Shell											*
 *																			*
\***************************************************************************/

/***************************************************************************
 * Includes
 ***************************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <mmsg.h>
#include <vfs.h>

/***************************************************************************
 * Types
 ***************************************************************************/

// Program execution request type
struct ProgramExecution {
	int msgcode; // backup message code
	char path[255]; // path to the executable
	int pathlen;
	char argv[2048]; // arguments - parsable by the kernel
	int argvlen;
	char env[1024]; // environment - parsable by the kernel
	int envlen;
};

/***************************************************************************
 * Functions
 ***************************************************************************/

// Executes a program
int new_execve( char* name, char* argv, char* env )
{
	// create the message
	struct ProgramExecution* pexec = (struct ProgramExecution*) malloc( sizeof( struct ProgramExecution ) );
	pexec->msgcode = 0x64;
	pexec->pathlen = sprintf( pexec->path, "%s", name );
	pexec->argvlen = sprintf( pexec->argv, "%s", argv );
	pexec->envlen = sprintf( pexec->env, "%s", env );
	
	// send to the kernel
	mattiseSendMessage( 0, 0x64, (char*) pexec, sizeof( struct ProgramExecution ) );
	
	// success
	return 0;
}

// Entry point
int main( int argc, char* argv[] )
{
	// current directory
	char* currdir = (char*) malloc( 512 );
	
	// fill it
	strcpy( currdir, "dr0/" );
	
	// print the entry stuff
	printf( "Mattise Shell\nCopyright 2007 Matthew Iselin\nMattise is ALPHA.\n" );
	
	// printf( "Sizeof process execution message is %d\n", sizeof( struct ProgramExecution ) );
	
	// main shell loop
	while( 1 )
	{
		// print the current directory
		printf( "%s@%s %s\n", "User", "MachName", currdir );
		
		//new_execve( "dr0/bin/testprog.bin", "", "" );
		
		//break;
		
		// input string
		char* usrinput = (char*) malloc( 1024 );
		
		// get a line
		gets( usrinput );
		
		// tell me
		printf( "'%s' is not recognized!\n", usrinput );
		
		// free the input string
		free( usrinput );
	}
	while( 1 );
	
	// return success
	return 0;
}
